﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VNWIKI_WEB.DAO;
using System.Web.Mail;

namespace VNWIKI_WEB.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Index()
        {
            return View();
        }

        public string SignIn(string accName, string password)
        {

            string userName;
            AccountDAO account = new AccountDAO();
            User user = account.Login(accName.Trim(), password.Trim());
            if (user != null)
            {
                Session["user"] = user;
                Session["userName"] = user.AccountName;
                userName = user.AccountName;
            }
            else
            {
                userName = "";
            }
            return userName;
        }

        public string SignUp(string accName, string password, string email)
        {
            string result;
            AccountDAO account = new AccountDAO();
            string name = GetCode();
            int rs = account.SignUp(name, accName.Trim(), password.Trim(), email.Trim());
            if (rs.Equals(1))
            {
                User user = account.GetUser(name, email);
                result = "Đăng ký thành công! Kiểm tra email để kích hoạt tài khoản.";
                //SendEmail(email, "Kích hoạt tài khoản từ vnwiki.com", "Xin chào");
            }
            else
            {
                result = "";
            }
            return result;
        }

        public ActionResult Logout()
        {
            Session["user"] = null;
            Session["userName"] = null;
            return RedirectToAction("Index", "Home");
        }

        

        //public void SendEmail(string address, string subject, string message)
        //{
        //    string email = "guitarlove9x@gmail.com";
        //    string password = "patuang09";

        //    var loginInfo = new NetworkCredential(email, password);
        //    var msg = new MailMessage();
        //    var smtpClient = new SmtpClient("smtp.gmail.com", 587);

        //    msg.From = new MailAddress(email);
        //    msg.To.Add(new MailAddress(address));
        //    msg.Subject = subject;
        //    msg.Body = message;
        //    msg.IsBodyHtml = true;

        //    smtpClient.EnableSsl = true;
        //    smtpClient.UseDefaultCredentials = false;
        //    smtpClient.Credentials = loginInfo;
        //    smtpClient.Send(msg);
        //    //System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        //    //message.To.Add("guitarlove9x@yahoo.com");
        //    //message.Subject = "This is the Subject line";
        //    //message.From = new System.Net.Mail.MailAddress("service@vnwiki.com");
        //    //message.Body = "This is the message body";
        //    //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.asia.secureserver.net");
        //    //smtp.Send(message);
        //}

        private void SendEmail(string address, string subject, string message)
        {
            const string SERVER = "relay-hosting.secureserver.net";
            MailMessage oMail = new System.Web.Mail.MailMessage();
            oMail.From = "service@vnbox.vn";
            oMail.To = address;
            oMail.Subject = subject;
            oMail.BodyFormat = MailFormat.Html;	// enumeration
            oMail.Priority = MailPriority.High;	// enumeration
            oMail.Body = message;
            SmtpMail.SmtpServer = SERVER;
            SmtpMail.Send(oMail);
            oMail = null;	// free up resources
        }

        public void ActiveAccount(int idUser, string name)
        {
            
        }

        public string GetCode()
        {
            string rs = "";
            Random ran = new Random();
            int num1 = ran.Next(1, 9);
            int num2 = ran.Next(1, 9);
            int num3 = ran.Next(1, 9);
            int num4 = ran.Next(1, 9);
            int num5 = ran.Next(1, 9);
            rs = num1.ToString() + num2.ToString() + num3.ToString() + num4.ToString() + num5.ToString();
            return rs;
        }
    }
}
